package com.example.tourpal;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.tourpal.model.LocalDataLoader;
import com.example.tourpal.model.User;

public class FollowSelectActivity extends Activity {

	private static final String UNDER_LINE = "-";
	private static final String UNDER_COMMA = ",";

	private Context context;

	// UI
	private LinearLayout followList;

	private TextView ok;
	private TextView cancel;

	private String[] spotNameList;
	private boolean allcheck;

	private User user;
	private int planUserId;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);

		context = this;

		setContentView(R.layout.follow_select_view);

		setupView();
	}

	private void setupView() {

		allcheck = false;

		spotNameList = getIntent().getStringExtra("spotNameList").split(
				UNDER_LINE);
		user = (User) getIntent().getSerializableExtra("user");
		planUserId = getIntent().getIntExtra("planUserId", 0);

		followList = (LinearLayout) findViewById(R.id.followList);

		LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
				LayoutParams.MATCH_PARENT, 100);
		for (int i = 0; i < spotNameList.length; i++) {
			CheckBox spotCheck = new CheckBox(this);
			spotCheck.setText(spotNameList[i]);
			spotCheck.setTextColor(0xff333333);
			spotCheck.setTextSize(20);

			spotCheck.setButtonDrawable(new ColorDrawable(Color.TRANSPARENT));
			Drawable childD = getResources().getDrawable(
					R.drawable.follow_selector);
			childD.setBounds(0, 0, childD.getMinimumWidth(),
					childD.getMinimumHeight());
			spotCheck.setCompoundDrawables(null, null, childD, null);

			followList.addView(spotCheck, lp);

			View line = new View(this);
			line.setBackgroundColor(0xffd2d9d7);
			line.setMinimumWidth(1);
			line.setMinimumHeight(1);

			followList.addView(line, new LinearLayout.LayoutParams(
					LayoutParams.MATCH_PARENT, 1));

		}

		CheckBox spotAllCheck = new CheckBox(this);
		spotAllCheck.setText(R.string.follow_all_spot);
		spotAllCheck.setTextColor(0xff333333);
		spotAllCheck.setTextSize(20);

		spotAllCheck.setButtonDrawable(new ColorDrawable(Color.TRANSPARENT));
		Drawable childD = getResources()
				.getDrawable(R.drawable.follow_selector);
		childD.setBounds(0, 0, childD.getMinimumWidth(),
				childD.getMinimumHeight());
		spotAllCheck.setCompoundDrawables(null, null, childD, null);
		spotAllCheck.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				for (int i = 0; i < followList.getChildCount() - 2; i++) {
					if (i % 2 == 0) {
						CheckBox sc = (CheckBox) followList.getChildAt(i);
						if (allcheck) {
							sc.setChecked(false);
						} else {
							sc.setChecked(true);
						}
					}
				}
				allcheck = !allcheck;
			}
		});

		followList.addView(spotAllCheck, lp);

		View line = new View(this);
		line.setBackgroundColor(0xffd2d9d7);
		line.setMinimumWidth(1);
		line.setMinimumHeight(1);

		followList.addView(line, new LinearLayout.LayoutParams(
				LayoutParams.MATCH_PARENT, 1));

		ok = (TextView) findViewById(R.id.ok);
		ok.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				String followInfo = "";
				for (int i = 0; i < followList.getChildCount() - 2; i++) {
					if (i % 2 == 0) {
						CheckBox sc = (CheckBox) followList.getChildAt(i);
						if (sc.isChecked()) {
							followInfo += UNDER_COMMA + sc.getText();
						}
					}
				}

				if (planUserId == 0) {
					Toast.makeText(context,
							"Send Messag To " + user.getUserName(),
							Toast.LENGTH_SHORT).show();
					return;
				}
				LocalDataLoader loader = new LocalDataLoader(context);

				// >>>post service
				if (!TextUtils.isEmpty(followInfo)) {
					String followString = String.format(getResources()
							.getString(R.string.folow_message), followInfo
							.substring(1));
					loader.startInsertOneMessageData(planUserId, followString,
							null);

					Toast.makeText(context, "Send Messag To " + planUserId,
							Toast.LENGTH_SHORT).show();
				}

				finish();
			}
		});

		cancel = (TextView) findViewById(R.id.cancel);
		cancel.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				finish();
			}
		});
	}

	@Override
	public void finish() {
		// TODO Auto-generated method stub
		super.finish();
		overridePendingTransition(R.anim.main_enter, R.anim.sub_exit);
	}
}
